home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C++
/
Frameworks
/
Sprocket Framework DR2
/
Sprocket Starter
/
SprocketStarter Code
/
UTextWindow.c
< prev
next >
Wrap
Text File
|
1996-06-15
|
7KB
|
296 lines
/*
File: UTextWindow.cp
Project: Sample code for Sprocket Framework 1.1 (DR2), released 6/15/96
Contains: Utilites for the WASTE based text editing window
To Do: ?
Sprocket Major Contributors:
----------------------------
Dave Falkenburg, producer of Sprocket 1.0
Bill Hayden, producer of Sprocket 1.1
Steve Sisak, producer of the upcoming Sprocket 2.0
Pete Alexander Steve Falkenburg Randy Thelen
Eric Berdahl Nitin Ganatra Chris K. Thomas
Marshall Clow Dave Hershey Leonard Rosenthal
Tim Craycroft Dave Mark Dean Yu
David denBoer Gary Powell
Cameron Esfahani Jon Summers Apple Computer, Inc.
Comments, Additions, or Corrections:
------------------------------------
Bill Hayden, Nikol Software <nikol@codewell.com>
*/
#ifndef __CONTROLS__
#include <Controls.h>
#endif
#ifndef __FIXMATH__
#include <FixMath.h>
#endif
#ifndef __TOOLUTILS__
#include <ToolUtils.h>
#endif
#include "limits.h"
#include "UTextWindow.h"
// long control auxiliary record used for keeping long settings
// a handle to this record is stored in the reference field of the control record
struct LCAuxRec
{
long value; // long value
long min; // long min
long max; // long max
};
typedef struct LCAuxRec LCAuxRec, *LCAuxPtr, **LCAuxHandle;
OSErr LCAttach( ControlRef control )
{
Handle aux;
LCAuxPtr pAux;
OSErr reply = noErr;
/* allocate the auxiliary record that will hold long settings */
aux = NewHandleClear( sizeof( LCAuxRec ) );
if ( aux == NULL )
{
reply = MemError();
return reply;
}
/* store a handle to the auxiliary record in the contrlRfCon field */
SetControlReference( control, (long)aux );
/* copy current control settings into the auxiliary record */
pAux = *((LCAuxHandle)aux);
pAux->value = GetControlValue( control );
pAux->min = GetControlMinimum( control );
pAux->max = GetControlMaximum( control );
return reply;
}
void LCDetach( ControlRef control )
{
Handle aux;
aux = (Handle)GetControlReference( control );
if ( aux != NULL )
{
SetControlReference( control, 0 );
DisposeHandle( aux );
}
return;
}
void LCSetValue( ControlRef control, long value )
{
LCAuxPtr pAux;
short controlMin, controlMax, newControlValue;
pAux = *((LCAuxHandle)GetControlReference( control ));
/* make sure value is in the range min...max */
if ( value < pAux->min )
value = pAux->min;
if ( value > pAux->max )
value = pAux->max;
/* save value in auxiliary record */
pAux->value = value;
/* calculate new thumb position */
controlMin = GetControlMinimum( control );
controlMax = GetControlMaximum( control );
newControlValue = controlMin + FixRound( FixMul ( FixDiv( value - pAux->min,
pAux->max - pAux->min), (controlMax - controlMin) << 16 ));
/* do nothing if the thumb position hasn't changed */
if ( newControlValue != GetControlValue(control) )
SetControlValue( control, newControlValue );
return;
}
void LCSetMin( ControlRef control, long min )
{
LCAuxPtr pAux;
pAux = *((LCAuxHandle)GetControlReference( control ));
/* make sure min is less than or equal to max */
if ( min > pAux->max )
min = pAux->max;
/* save min in auxiliary record */
pAux->min = min;
/* set contrlMin field to min or SHRT_MIN, whichever is greater */
if ( min < SHRT_MIN )
min = SHRT_MIN;
SetControlMinimum( control, min );
/* reset value */
LCSetValue( control, pAux->value );
return;
}
void LCSetMax( ControlRef control, long max )
{
LCAuxPtr pAux;
pAux = *((LCAuxHandle)GetControlReference( control ));
/* make sure max is greater than or equal to min */
if ( max < pAux->min )
max = pAux->min;
/* save max in auxiliary record */
pAux->max = max;
/* set contrlMax field to max or SHRT_MAX, whichever is less */
if ( max > SHRT_MAX )
max = SHRT_MAX;
SetControlMaximum( control, max );
/* reset value */
LCSetValue( control, pAux->value );
return;
}
/* In each of these LCGetXXX() functions, there are 2 ways listed to do things. They are
both the same thing and perform the same stuff, just one is easier to read than the
other (IMHO). I asked Marco about it and he gave me the shorter code (what's commented
in each function) and gave me this explanation:
This version [the commented code] yields smaller and faster code
(try disassembling both versions if you wish), but some people may
find it somewhat harder to read.
I agree with Marco that his code is better overall, but in the interest of readabilty
(since this demo is a learning tool), I left my code in and put Marco's in commented
out. Pick whichever you'd like to use.
*/
long LCGetValue( ControlRef control )
{
//LCAuxPtr pAux;
//pAux = *((LCAuxHandle)GetControlReference( control ));
//return pAux->value;
// this is Marco's code. Remember, this is a little harder to read, but overall
// yields tighter code.
return (* (LCAuxHandle) GetControlReference(control)) -> value;
}
long LCGetMin( ControlRef control )
{
//LCAuxPtr pAux;
//pAux = *((LCAuxHandle)GetControlReference( control ));
//return pAux->min;
// this is Marco's code. Remember, this is a little harder to read, but overall
// yields tighter code.
return (* (LCAuxHandle)GetControlReference(control)) -> min;
}
long LCGetMax( ControlRef control )
{
//LCAuxPtr pAux;
//pAux = *((LCAuxHandle)GetControlReference( control ));
//return pAux->max;
// this is Marco's code. Remember, this is a little harder to read, but overall
// yields tighter code.
return (* (LCAuxHandle)GetControlReference(control)) -> max;
}
void LCSynch( ControlRef control )
{
LCAuxPtr pAux;
short controlMin, controlMax, controlValue;
controlMin = GetControlMinimum( control );
controlMax = GetControlMaximum( control );
controlValue = GetControlValue( control );
pAux = *((LCAuxHandle)GetControlReference( control ));
/* calculate new long value */
pAux->value = pAux->min + FixMul( FixRatio ( controlValue - controlMin,
controlMax - controlMin), pAux->max - pAux->min );
return;
}
// If MoreFiles is added to Sprocket, then this can get tossed
OSErr CheckObjectLock(short vRefNum, long dirID, StringPtr name)
{
CInfoPBRec pb;
OSErr error;
pb.hFileInfo.ioNamePtr = name;
pb.hFileInfo.ioVRefNum = vRefNum;
pb.hFileInfo.ioDirID = dirID;
pb.hFileInfo.ioFDirIndex = 0; // use ioNamePtr and ioDirID
error = PBGetCatInfoSync(&pb);
if ( error == noErr )
{
// check locked bit
if ( (pb.hFileInfo.ioFlAttrib & 0x01) != 0 )
error = fLckdErr;
}
return ( error );
}